//
//  menuScrollView.m
//  BeautifulLockerRoom
//
//  Created by mac on 16/7/28.
//  Copyright © 2016年 Winny. All rights reserved.
//

#import "menuScrollView.h"

UILabel * label;
NSMutableArray * allButton;
@implementation menuScrollView


        

-(instancetype)initWithUrl:(NSString *)url
{
    self = [super init];
    if (self) {
        
        self = [[menuScrollView alloc]initWithFrame:CGRectMake(0, 64, WINDOWWIDTH - 40, 40)];
        self.backgroundColor = [UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1];
        
        allButton = [NSMutableArray array];
        label = [[UILabel alloc]initWithFrame:CGRectMake(5, 38, 50, 2)];
        label.backgroundColor = [UIColor redColor];
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.contentSize.width, 40)];
        self.showsHorizontalScrollIndicator = NO;
        image.image = [UIImage imageNamed:@"alertViewCustom_left"];
        image.backgroundColor = [UIColor redColor];
        [self addSubview:image];
        [RequestTool getNetworkRequestWithUrl:url date:^(NSDictionary * dic)
         {
             [self menuSVChildViewsWithDic:dic];
         }];
        self.directionalLockEnabled = YES;
    }
    return self;
}
/*
-(void)reciveNotice:(NSNotification *)notification
{
    NSInteger tag = [[notification.userInfo objectForKey:@"tag"]integerValue];
    [UIView animateWithDuration:0.1 animations:^{   
        if (tag * 60 +30 < 20*60) {
            self.label.center = CGPointMake(tag*60+30, 38);
 }
    }];
}
 */
-(void)menuSVChildViewsWithDic:(NSDictionary *)dic
{
    NSDictionary * data = [dic objectForKey:@"data"];
    NSArray * array = [data objectForKey:@"floor_items"];
    buttonTitleArray = [NSMutableArray array];
    for (int i = 0; i<array.count; i++)
    {
        NSDictionary * itemDic = array[i];
        NSString * muneTitle = [itemDic objectForKey:@"title"];
        [buttonTitleArray addObject:muneTitle];
    }
    self.contentSize = CGSizeMake(buttonTitleArray.count * 60 , -64);
    for (int i = 0; i < buttonTitleArray.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1];
        button.frame = CGRectMake(i*60, 0, 60, 40);
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        if (i == 0)
        {
            _btn = button;
            [_btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        [allButton addObject:button];
        [self addSubview:button];
        /*
        NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(reciveNotice:) name:@"notice1" object:nil];
         */
    }
    [self addSubview:label];
}
#pragma mark - 按钮点击
-(void)buttonClick:(UIButton *)sender
{
    
    NSNotification * notification = [NSNotification notificationWithName:@"notice" object:nil userInfo:@{@"tag":@(sender.tag)}];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    [UIView animateWithDuration:0.25 animations:^{
        if (sender.center.x <= WINDOWWIDTH/2) {
            self.contentOffset = CGPointZero;
        }
        else if (sender.center.x >= self.contentSize.width - WINDOWWIDTH/2 )
        {
            self.contentOffset = CGPointMake(self.contentSize.width - WINDOWWIDTH+44 , 0);
        }
        else
        {
            self.contentOffset = CGPointMake(sender.center.x - WINDOWWIDTH/2, 0);
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            label.center = CGPointMake(sender.center.x, 38);
        }];
    }];
    
    [_btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _btn = sender;
}

+(void)contentVCMoveWithOffset:(CGFloat)offset
{
    for (UIButton * btn in allButton)
    {
        if (btn.tag == offset/WINDOWWIDTH)
        {
            menuScrollView * sV = (menuScrollView *)btn.superview;
            [sV buttonClick:btn];
        }
    }
    label.center = CGPointMake(offset/WINDOWWIDTH*60+30, 38);
    
}
@end
